package com.wu.creditmanager.adapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import com.wu.creditmanager.R;
import com.wu.creditmanager.entity.Bill;

public class BillAdapter extends BaseAdapter {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private final List<Bill> bills;
	private Context context;
	private boolean showPayState = false;

	public BillAdapter(Context context, List<Bill> bills) {
		this.context = context;
		this.bills = bills;
	}

	public int getCount() {
		return bills.size();
	}

	public Object getItem(int position) {
		return bills.get(position % bills.size());
	}

	public long getItemId(int position) {
		return bills.get(position % bills.size()).getId();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater mInflater = LayoutInflater.from(context);
			convertView = mInflater.inflate(R.layout.bill_list_item, null);
			holder = new ViewHolder();
			holder.creditName = (TextView) convertView
					.findViewById(R.id.notify_credit_name);
			holder.statementDay = (TextView) convertView
					.findViewById(R.id.notify_statementday);
			holder.paymentDueDay = (TextView) convertView
					.findViewById(R.id.notify_payment_due_day);
			holder.amountPayable = (TextView) convertView
					.findViewById(R.id.notify_amountpayable);
			holder.icon = (ImageView) convertView
					.findViewById(R.id.notify_icon);
			holder.payState = (TextView) convertView
					.findViewById(R.id.notify_pay_state);
			holder.payStateRow = (TableRow) convertView
					.findViewById(R.id.notify_pay_state_row);
			holder.payTime = (TextView) convertView
					.findViewById(R.id.notify_pay_time);
			holder.payAmountRow = (TableRow) convertView
					.findViewById(R.id.notify_pay_amount_row);
			holder.payAmount = (TextView) convertView
					.findViewById(R.id.notify_pay_amount);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final Bill info = bills.get(position % bills.size());
		holder.icon.setImageResource(info.getCredit().getBank().getIconResId());
		holder.creditName.setText(info.getCredit().getName());
		holder.statementDay
				.setText(sdf.format(new Date(info.getStatementDay())));
		String paymentDueDay = sdf.format(new Date(info.getPaymentDueDay()));
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		long leftdays = ((info.getPaymentDueDay() - calendar.getTimeInMillis()) / 1000 / 60 / 60 / 24) + 1;
		Resources resources = context.getResources();
		String leftdayStr = leftdays >= 0 ? String.format(
				resources.getString(R.string.leftdays), leftdays) : String
				.format(resources.getString(R.string.leftdays2), -leftdays);
		holder.paymentDueDay.setText(info.isPaid() ? paymentDueDay : String
				.format("%1$s(%2$s)", paymentDueDay, leftdayStr));
		holder.amountPayable.setText(info.getAmountPayableString(resources
				.getString(R.string.no_amount_payable)));
		if (showPayState) {
			holder.payStateRow.setVisibility(View.VISIBLE);
			holder.payState.setText(info.isPaid() ? resources
					.getString(R.string.pay_state_paid) : resources
					.getString(R.string.pay_state_notpaid));
			holder.payAmountRow.setVisibility(View.VISIBLE);
			if (info.isPaid()) {
				holder.payTime.setText(sdf.format(new Date(info.getPaytime())));
				holder.payAmount.setText(info.getPayAmountString());
			}else{
				holder.payAmountRow.setVisibility(View.GONE);
			}
		} else {
			holder.payStateRow.setVisibility(View.GONE);
			holder.payAmountRow.setVisibility(View.GONE);
		}
		return convertView;
	}

	class ViewHolder {
		ImageView icon;
		TextView creditName;
		TextView statementDay;
		TextView amountPayable;
		TextView paymentDueDay;
		TableRow payStateRow;
		TextView payState;
		TableRow payAmountRow;
		TextView payAmount;
		TextView payTime;
	}

	public boolean isShowPayState() {
		return showPayState;
	}

	public void setShowPayState(boolean showPayState) {
		this.showPayState = showPayState;
	}

}